package com.tjetc.view.adminview;

import com.tjetc.proxy.CilentProxy;
import com.zhiyun.domain.Role;
import com.zhiyun.service.RoleService;
import com.zhiyun.vo.Result;

import java.util.List;
import java.util.Scanner;

public class Admins_UserClass {
    RoleService roleService= CilentProxy.getProxyInstance(RoleService.class);
    Scanner scanner=new Scanner(System.in);
    public void start() {
        while (true){
            System.out.println("~~~~~~~~~用户类型管理~~~~~~~~~");
            System.out.println("1. 全部用户类型");
            System.out.println("2. 增加用户类型");
            System.out.println("3. 删除用户类型");
            System.out.println("4. 修改用户类型");
            System.out.println("0. 退出");
            int i = scanner.nextInt();
            switch (i){
                case 1:
                    allRole();
                    break;
                case 2:
                    insertRole();
                    break;
                case 3:
                    delRole();
                    break;
                case 4:
                    updateRole();
                    break;
                case 0:
                    return;
                default:
                    break;
            }
        }
    }

    private void allRole() {
        while (true){
            Result all = roleService.findAll();
            if (all.getCode()==200){
                System.out.println(all.getMsg());
                List<Role> obj = (List<Role>) all.getObj();
                for (Role role : obj) {
                    System.out.println("id:"+role.getId()+"          用户类型名称:"+role.getName());
                }
            }else {
                System.out.println(all.getMsg());
            }
            break;
        }
    }

    private void insertRole() {
        while (true){
            System.out.println("请输入用户类型名称");
            String name = scanner.next();
            Result insert = roleService.insert(new Role(null, name));
            if (insert.getCode()==200){
                System.out.println(insert.getMsg());
            }else {
                System.out.println(insert.getMsg());
            }
            break;
        }
    }

    private void delRole() {
        while (true){
            Result all = roleService.findAll();
            if (all.getCode()==200){
                System.out.println(all.getMsg());
                List<Role> obj = (List<Role>) all.getObj();
                for (Role role : obj) {
                    System.out.println("id:"+role.getId()+"          用户类型名称:"+role.getName());
                }
                System.out.println("请输入想要删除的用户类型id");
                int i = scanner.nextInt();
                Result delete = roleService.delete(i);
                if (delete.getCode()==200){
                    System.out.println(delete.getMsg());
                }else {
                    System.out.println(delete.getMsg());
                }
            }else {
                System.out.println(all.getMsg());
            }
            break;
        }
    }

    private void updateRole() {
        while (true){
            Result all = roleService.findAll();
            if (all.getCode()==200){
                System.out.println(all.getMsg());
                List<Role> obj = (List<Role>) all.getObj();
                for (Role role : obj) {
                    System.out.println("id:"+role.getId()+"          用户类型名称:"+role.getName());
                }
                System.out.println("请输入想要修改的用户类型id");
                int i = scanner.nextInt();
                System.out.println("请输入修改后的用户类型名称");
                String name = scanner.next();
                Role role = new Role(i, name);
                Result update = roleService.update(role);
                if (update.getCode()==200){
                    System.out.println(update.getMsg());
                }else {
                    System.out.println(update.getMsg());
                }
            }else {
                System.out.println(all.getMsg());
            }
            break;
        }
    }
}
